package h0803;

import java.util.Scanner;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 最长指定瑕疵度的元音子串
 * @date 2024/8/3 17:08
 */
public class TheLongestFlawsVowelSubstring2 {
    static String vowels = "aeiouAEIOU";

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //瑕疵度
        int allowedFlaws = sc.nextInt();

        sc.nextLine(); // 消费换行符
        String s = sc.nextLine();
        System.out.println(findLongestVowelSubstring(s, allowedFlaws));
    }

    private static int findLongestVowelSubstring(String s, int allowedFlaws) {
        int n = s.length();
        int start = 0;
        int flawCount = 0;

        int ans = 0;
        for (int end = 0; end < n; end++) {
            char currentChar = s.charAt(end);
            //根据瑕疵计数改变窗口，始终保持窗口在合理的范围内
            if (!vowels.contains(String.valueOf(currentChar))) {
                flawCount++;
            }
            while (flawCount > allowedFlaws) {
                if (!vowels.contains(String.valueOf(s.charAt(start)))) {
                    flawCount--; //如果不在开始字符不在vowels里面，瑕疵度减一
                }
                start++; //窗口缩小
            }
            String temp = s.substring(start, end + 1);
            if (!temp.isEmpty() && isVowel(temp, allowedFlaws)) {
                ans = Math.max(temp.length(), ans);
            }
        }
        return ans;
    }

    //判断字符串合法性
    private static boolean isVowel(String temp, int flaws) {
        int n = temp.length();
        if (!vowels.contains(String.valueOf(temp.charAt(0))) || !vowels.contains(String.valueOf(temp.charAt(n - 1)))) {
            return false;
        }

        //TODO 判断是否满足指定瑕疵度
        int count = 0;
        for (int i = 1; i < n - 1; i++) {
            char c = temp.charAt(i);
            //不是元音字母
            if (count > flaws) {
                return false;
            } else if (!vowels.contains(String.valueOf(c))) {
                count++; //TODO 这里统计的次数有可能下一次循环就结束了，所以要在最后判断是否仍然count==flaws;
            }
        }
        return count == flaws;
    }
}
